package com.epro.dx.enable;

import java.util.HashMap;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Gallery;
import android.widget.ImageView;

/**
 * 图片浏览句柄对象
 * 用于子项异步读取图片
 * @author chenxiaolei/sai`kopui
 * @id nj728
 */
public class ProductImageBrowsHandler extends Handler
{
    /**
     * 消息
     */
    public final static int MSG_START = 0;
    
    /**
     * message封装图片地址的键
     */
    public final static String IMAGE_VIEW_URL = "imageViewUrl";
    
    /**
     * 图片map
     */
    private HashMap<String, Bitmap> bitmapList;
    
    /**
     * 普通缩略图片的大小
     */
    private int commonSize = 70;
    
    public ProductImageBrowsHandler(HashMap<String, Bitmap> bitmapList)
    {
        this.bitmapList = bitmapList;
    }
    
    @Override
    public void handleMessage(Message msg)
    {
        int what = msg.what;
        
        Bundle data = msg.getData();
        
        //获取消息中封装的对象
        Object objectRcvd = msg.obj;
        
        if (objectRcvd == null || !(objectRcvd instanceof ImageView))
        {
            return;
        }
        
        //强转为预期的ImageView
        ImageView iv = (ImageView)objectRcvd;
        
        //获取图片地址对象
        String imageUrl = data.getString(IMAGE_VIEW_URL);
        if (TextUtils.isEmpty(imageUrl))
        {
            return;
        }
        if (what == MSG_START)
        {
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setLayoutParams(new Gallery.LayoutParams(commonSize, commonSize));
            iv.setImageBitmap(bitmapList.get(imageUrl));
        }
        
        super.handleMessage(msg);
    }
}
